package com.antlr4.app.myantlr4;

import com.antlr4.DemoLexer;
import com.antlr4.DemoParser;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

/**
 * @Author xiongmin
 * @Description TODO
 * @Date 2019/11/15 14:45
 * @Version 1.0
 **/
public class MyAntlr4Test {
    public static void main(String[] args) throws Exception{

        // 通过键盘输入要分析字符串
//        CharStream input = args.length > 0? CharStreams.fromFileName(args[0]): CharStreams.fromStream(System.in);
//        MyAntlr4Lexer lexer = new MyAntlr4Lexer(input);

        String[] testStr={
                "xiongmin:age=22,action=hahha"
        };

        for (String s:testStr){
            System.out.println("Input expr:"+s);
            run(s);
        }
    }

    public static void run(String expr) throws Exception{

        //对每一个输入的字符串，构造一个 ANTLRStringStream 流 in
        ANTLRInputStream in = new ANTLRInputStream(expr);




        //用 in 构造词法分析器 lexer，词法分析的作用是产生记号
        MyAntlr4Lexer lexer = new MyAntlr4Lexer(in);

        //用词法分析器 lexer 构造一个记号流 tokens
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        //再使用 tokens 构造语法分析器 parser,至此已经完成词法分析和语法分析的准备工作
        MyAntlr4Parser parser = new MyAntlr4Parser(tokens);

        //最终调用语法分析器的规则 prog，完成对表达式的验证
        parser.first();
    }
}
